package com.powergisol.gis.service;

import com.powergisol.gis.dao.UserDao;
import com.powergisol.gis.model.Result;
import com.powergisol.gis.model.UserModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created By marc
 * Date: 2020/6/10  16:33
 * Description:
 */
@Service
public class UserServiceImpl {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();
    @Resource
    private UserDao userDao;

    public Result upPassword(String oldPassword, String newPassword, String userName) {
        Result result = new Result();
        UserModel tabUser = userDao.findByUsername(userName);
        if(null != tabUser && StringUtils.isNotBlank(newPassword) && StringUtils.isNotBlank(oldPassword)){
            if (!ENCODER.matches(oldPassword, tabUser.getPassword())) {
                return result.failure(-1,"原始密码输入错误");
            }
            if (ENCODER.matches(newPassword, tabUser.getPassword())) {
                return result.failure(-1,"新旧密码相同");
            }else{
                tabUser.setPassword(ENCODER.encode(newPassword));
                userDao.saveAndFlush(tabUser);
                result.success("修改成功");
                System.out.println("userName="+userName);
            }
        }else{
            return result.failure(-1,"信息不能为空");
        }
        return result;
    }

}
